package biz.noip.strategy.players;

import org.andengine.extension.tmx.TMXLayer;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.color.Color;

import biz.noip.strategy.mediator.MediatorGame;
import biz.noip.strategy.players.bars.HealthBar;
import biz.noip.strategy.scenes.GameScene;
import biz.noip.strategy.util.Coord;

public class WarriorPlayer extends Player {

	private static final int DAMAGE_MAX = 128;
	private static final int DAMAGE_MIN = 50;

	private static final int MOVE_MAX = 5;

	public static final int MAX_HEALTH = 500;
	private static final int ARMOR = 9;
	private static final float CRIT = 0.15f;
	private int damage;

	public WarriorPlayer(int startX, int startY, int pWidth, int pHeight, ITiledTextureRegion pTiledTextureRegion,
			VertexBufferObjectManager pVertexBufferObjectManager, GameScene gameScene, int team, TMXLayer mTmxLayer) {
		super(startX, startY, pWidth, pHeight, pTiledTextureRegion, pVertexBufferObjectManager, gameScene, team);
		setMove(MOVE_MAX);
		setHealthBar(new HealthBar(this, Color.RED, MAX_HEALTH, pVertexBufferObjectManager));
		registerUpdateBar(getHealthBar());
		setHealth(MAX_HEALTH);
	}

	@Override
	public boolean attack(Player player) {
		if (Math.abs(getGridX() - Coord.getNumGridXByCoord(player.getX())) <= 1
				&& Math.abs(getGridY() - Coord.getNumGridYByCoord(player.getY())) <= 1) {
			damage = getDamage(DAMAGE_MIN, DAMAGE_MAX, player.getArmor(), CRIT, getMove());
			mDamageView.setText(((Integer) damage).toString());
			if(isCrit()){
				mDamageView.setTextColor(Color.RED);
				setCrit(false);
			}
			player.isReceivedDamage(damage);
			mDamageView.show(player.getX(), player.getY());
			setSelect(false);
			setMove(0);
			return true;
		}
		return false;
	}

	@Override
	public int getDamage() {
		return damage;
	}

	@Override
	public void death() {
		scene.unregisterTouchArea(this);
		getHealthBar().removeBar();
		scene.getGame().deathPlayer(this);
		MediatorGame.getInstance().deleteChild(this, scene);
	}

	@Override
	public int getMaxMove() {
		return MOVE_MAX;
	}

	@Override
	public int getMaxDamage() {
		return DAMAGE_MAX;
	}

	@Override
	public int getMinDamage() {
		return DAMAGE_MIN;
	}

	@Override
	public int getMaxHealth() {
		return MAX_HEALTH;
	}
	
	@Override
	public int getArmor() {
		return ARMOR;
	}
}
